/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.Serializable;

/**
 *
 * @author riso
 */
public class Skore_zoznam implements Serializable {

    polozka zac;
    polozka kon;

    private static class polozka implements Serializable {

        polozka pred;
        polozka dalsia;
        String meno;
        int skore;

        public polozka(String meno, int skore) {
            pred = null;
            dalsia = null;
            this.meno = meno;
            this.skore = skore;

        }
        @Override
        public String toString() {

            StringBuilder sb = new StringBuilder();
            sb.append(meno);
            sb.append(": ");
            sb.append(skore);
            sb.append(" " + "\n");
            return sb.toString();
        }
    }

    public Skore_zoznam() {
        zac = null;
        kon = null;
    }

    public void vymaz(int cislo) {
        polozka pom = zac;
        for (int i = 0; i <= cislo - 1; i++) {
            pom = pom.dalsia;
        }
        if (zac == kon) {
            zac = null;
            kon = null;
        } else if (pom.pred == null) {
            zac = pom.dalsia;
            pom.dalsia.pred = null;
        } else if (pom.dalsia == null) {
            kon = pom.pred;
            pom.pred.dalsia = null;
        } else {
            pom.dalsia.pred = pom.pred;
            pom.pred.dalsia = pom.dalsia;
        }
    }

    public void pridaj(String meno, int skore) {
        polozka novy = new polozka(meno, skore);
        if (zac == null) {
            zac = novy;
            kon = novy;
        } else {
            polozka pom = zac;
            while (pom != null && novy.skore<=pom.skore) {
                pom = pom.dalsia;
            }
            if (pom == zac) {
                zac.pred = novy;
                novy.dalsia = zac;
                zac = novy;
            } else if (pom == null) {
                kon.dalsia = novy;
                novy.pred = kon;
                kon = novy;
            } else {
                pom.pred.dalsia = novy;
                novy.dalsia = pom;
                novy.pred = pom.pred;
                pom.pred = novy;
            }


        }


    }

int poradie=1;
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        polozka pom = zac;
        while (pom != null) {
            sb.append(poradie+". ");
            sb.append(pom.meno);
            sb.append(": ");
            sb.append(pom.skore);
            sb.append(" " + "\n");
            pom = pom.dalsia;
            poradie++;
        }
        poradie=1;
        return sb.toString();
    }
}
